﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameStateManagement.Screens.Inherbit
{
    public class StarBackgroundScreen : GameScreen
    {

        const int DefaultBufferSize = 500;
        VertexDeclaration vertexDeclaration;
        BasicEffect basicEffect;
        VertexPositionColor[] vertices = new VertexPositionColor[DefaultBufferSize];
        Random rand = new Random();


        public StarBackgroundScreen()
        {
            TransitionOnTime = TimeSpan.FromSeconds(.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);
        }


        public override void LoadContent()
        {
            base.LoadContent();
            GraphicsDevice GraphicsDevice = ScreenManager.Game.GraphicsDevice;
            int iWidth = GraphicsDevice.Viewport.Width;
            int iHeight = GraphicsDevice.Viewport.Height;

            for (int i = 0; i < DefaultBufferSize; i++)
            {
                vertices[i] = new VertexPositionColor();
                vertices[i].Position.X = rand.Next(iWidth);
                vertices[i].Position.Y = rand.Next(iHeight);
                vertices[i].Color = Color.White; //new Color(128, 128, 128);
            }
            

            // create a vertex declaration, which tells the graphics card what kind of
            // data to expect during a draw call. We're drawing using
            // VertexPositionColors, so we'll use those vertex elements.

            // set up a new basic effect, and enable vertex colors.
            basicEffect = new BasicEffect(GraphicsDevice);
            basicEffect.VertexColorEnabled = true;

            // projection uses CreateOrthographicOffCenter to create 2d projection
            // matrix with 0,0 in the upper left.
            basicEffect.Projection = Matrix.CreateOrthographicOffCenter
                (0, iWidth,
                iHeight, 0,
                0, 2);

        }

        public override void Update(Microsoft.Xna.Framework.GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            byte iRGB;
            for (int i = 0; i < DefaultBufferSize; i++)
                if ((rand.Next() % 88) == 0)
                {
                    iRGB = (byte)(rand.Next(256));
                    vertices[i].Color = new Color(iRGB, iRGB, iRGB);
                }

        }

        public override void Draw(GameTime gameTime)
        {
            
            //g.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.p
            base.Draw(gameTime);
        }


    }
}
